<template>
  <div class="roleManage">
<!--     <div class="tb-head">
      <el-button icon="plus" @click="handleAdd">新建角色</el-button>
    </div> -->
    <el-table v-loading="tbLoading" element-loading-text="拼命加载中" :data="roles" stripe style="width: 100%" class="table">
      <el-table-column prop="cName" label="角色" className="is-center">
      </el-table-column>
      <el-table-column label="角色用户数" className="is-center">
        <template slot-scope="scope">
          <el-button type="text" @click="$router.push({name: 'User', params: {cid: scope.row.cid}})">{{scope.row.cUserNum}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" className="is-center">
        <template slot-scope="scope">
          <el-button size="small" type="text" @click="handleEdit(scope.row.cid)">编辑</el-button>
          <el-button v-if="!scope.row.cUserNum" size="small" type="text" class="red" @click="handleDel(scope.row.cid)">删除</el-button>
          <el-tooltip v-else class="item" effect="dark" content="该角色下有用户，不可删除" placement="bottom" :enterable="false">
            <span class="btn-wrap"><el-button size="small" type="text" class="red" disabled>删除</el-button></span>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加/编辑弹层 -->
    <el-dialog :title="dialogName" :visible.sync="dialogVisible" custom-class="auth-dialog">
      <el-form :model="roleDetail" label-width="70px">
        <el-form-item label="角色名称">
          <el-input v-model="roleDetail.cName" auto-complete="off"></el-input>
        </el-form-item>
        <auth-list ref="AList" :auths="authData" :roleAuth="roleDetail.authority"></auth-list>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="saving">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import { mapState, mapActions } from 'vuex'
  import authList from '@/components/common/sysManage/authList'
  export default {
    data () {
      return {
        dialogName: '添加角色',
        dialogVisible: false,
        tbLoading: true,
        saving: false
      }
    },
    activated () {
      this.getRoleList().then(() => {
        this.tbLoading = false
      })
      this.getAuthList()
    },
    components: {
      authList
    },
    computed: {
      ...mapState({
        roles: state => state.sysManage.roles,
        authData: state => state.sysManage.authList,
        roleDetail: state => state.sysManage.roleDetail
      })
    },
    methods: {
      ...mapActions(['getRoleList', 'getRoleDetail', 'getAuthList', 'addRole', 'delRole', 'editRole']),
      // 新增
      handleAdd: function () {
        this.dialogName = '新建角色'
        this.dialogVisible = true
        this.roleDetail.cName = ''
        this.roleDetail.authority = ''
      },
      // 编辑
      handleEdit: function (cid) {
        this.dialogName = '编辑角色'
        this.dialogVisible = true
        this.$data.cid = cid
        this.getRoleDetail(cid)
      },
      // 提交
      handleSubmit: function () {
        let name = this.roleDetail.cName
        let authority = this.$refs.AList.checkedList.join(',')
        this.saving = true
        if (this.dialogName === '新建角色') {
          this.addRole({name, authority})
            .then((res) => {
              this.saving = false
              if (res.data.code === 1) {
                this.dialogVisible = false
                this.$message({
                  type: 'success',
                  message: res.data.msgZ
                })
                this.getRoleList()
              }
            }).catch(() => { this.saving = false })
        } else {
          let cid = this.$data.cid
          this.editRole({cid, name, authority})
            .then(res => {
              this.saving = false
              if (res.data.code === 1) {
                this.dialogVisible = false
                this.getRoleList()
              }
            }).catch(() => { this.saving = false })
        }
      },
      // 删除
      handleDel: function (cid) {
        this.$confirm('是否确认删除？', '提示', {
          type: 'warning'
        }).then(() => {
          return this.delRole(cid)
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getRoleList()
        }).catch(() => {})
      }
    }
  }
</script>
<style lang="scss">
  .roleManage{
    .btn-wrap{margin-left:10px;}
  }
  .auth-dialog{width:640px;}
  .auth-dialog .el-form{height:618px;overflow-y: scroll}
</style>
